package eoeProject.edenOfEast.core;

import java.text.SimpleDateFormat;
import java.util.Date;

import eoeProject.edenOfEast.bean.DBBean;

public class SQLMaker {

	private StringBuffer sql = new StringBuffer();
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	int countAddSetValue = 0;

	public static final String SQL_TYPE_SELECT = "SELECT";
	public static final String SQL_TYPE_INSERT = "INSERT";
	public static final String SQL_TYPE_UPDATE = "UPDATE";
	public static final String SQL_TYPE_DELETE = "DELETE";

	public SQLMaker(String tableName, String sqlType) {
		if (sqlType.equals(SQL_TYPE_SELECT))
			sql.append(sqlType + " * FROM " + tableName);
		else if (sqlType.equals(SQL_TYPE_INSERT))
			sql.append(sqlType + " INTO " + tableName);
		else if (sqlType.equals(SQL_TYPE_UPDATE))
			sql.append(sqlType + " " + tableName + " SET ");
		else if (sqlType.equals(SQL_TYPE_DELETE))
			sql.append(sqlType + " FROM " + tableName + " ");
	}

	public void setExp(SQLExp exp) {
		sql.append(" WHERE " + exp.getExpValue() + " ");
	}

	public void addExpNotNull(String column) {
		if (sql.toString().indexOf("WHERE") == -1) {
			sql.append(" WHERE " + column + " is not null ");
		} else {
			sql.append("AND " + column + " is not null ");
		}
	}

	public void addExpEquals(String column, String value) {
		if (sql.toString().indexOf("WHERE") == -1) {
			sql.append(" WHERE " + column + " is not null AND " + column
					+ " = '" + value + "' ");
		} else {
			sql.append("AND " + column + " is not null AND " + column + " = '"
					+ value + "' ");
		}
	}

	public void addExpEquals(String column, Date value) {
		addExpEquals(column, sdf.format(value));
	}

	public void addExpEquals(String column, Integer value) {
		addExpEquals(column, value.toString());
	}

	public void addExpNotEquals(String column, String value) {
		if (sql.toString().indexOf("WHERE") == -1) {
			sql.append(" WHERE " + column + " is not null AND " + column
					+ " != '" + value + "' ");
		} else {
			sql.append("AND " + column + " is not null AND " + column + " != '"
					+ value + "' ");
		}
	}

	public void addExpNotEquals(String column, Date value) {
		addExpNotEquals(column, sdf.format(value));
	}

	public void addExpNotEquals(String column, Integer value) {
		addExpNotEquals(column, value.toString());
	}

	public void addLimit(Integer limit) {
		sql.append("limit " + limit);

	}

	public void addOrderBy(String column) {
		sql.append("order by  " + column + " ");
	}

	public void addOrderBy(String column, String sort) {
		sql.append("order by  " + column + " " + sort + " ");
	}

	public void addSetValue(String column, String value) {
		if (countAddSetValue > 0)
			sql.append(", " + column + " = '" + value + "' ");
		else
			sql.append(column + " = '" + value + "' ");
		countAddSetValue++;
	}

	public void addSetValue(String column, Date value) {
		addSetValue(column, sdf.format(value));
	}

	public void addSetValue(String column, Integer value) {
		addSetValue(column, value.toString());
	}

	private void setColumn(DBBean bean) {
		sql.append("( ");
		for (int i = 0; i < bean.getColumnList().size() - 1; i++) {
			sql.append(bean.getColumnList().get(i) + ", ");
		}
		sql.append(bean.getColumnList().get(bean.getColumnList().size() - 1)
				+ ") ");
	}

	private void setValues(DBBean bean) {
		bean.setValueList();
		sql.append("VALUES ( ");
		for (int i = 0; i < bean.getValueList().size() - 1; i++) {
			sql.append("'" + bean.getValueList().get(i) + "' , ");
		}
		sql.append("'"
				+ bean.getValueList().get(bean.getValueList().size() - 1)
				+ "') ");
	}

	public void setInsert(DBBean bean) {
		setColumn(bean);
		setValues(bean);
	}

	public String getSQL() {
		return sql.toString();
	}

}
